package com.dayu.gulimall.cart.vo;

import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author dayu
 * @version 1.0
 * @email dayu007@foxmail.com
 * @date 2021/3/26 7:23
 * @description 整个购物车，需要计算的属性必须重写，保证每次获取都会重新计算
 */
public class Cart {
    List<CartItem> items;
    private Integer countNum;//商品数量
    private Integer countType; //商品类型数量
    private BigDecimal totalAmount;//商品总价
    private BigDecimal reduce = new BigDecimal("0"); //商品减免价格

    public List<CartItem> getItems() {
        return items;
    }

    public void setItems(List<CartItem> items) {
        this.items = items;
    }

    public Integer getCountNum() {
        int num = 0;
        if (!CollectionUtils.isEmpty(items)) {
            for (CartItem item : items) {
                num = num + item.getCount();
            }
        }
        return num;
    }

    public Integer getCountType() {
        int num = 0;
        if (!CollectionUtils.isEmpty(items)) {
            for (CartItem item : items) {
                num += 1;
            }
        }
        return num;
    }


    public BigDecimal getTotalAmount() {
        BigDecimal amount = new BigDecimal("0");
        //计算购物项总价
        if (!CollectionUtils.isEmpty(items)) {
            for (CartItem item : items) {
                if (item.getCheck()) {
                    BigDecimal totalAmount = item.getTotalPrice();
                    amount = amount.add(totalAmount);
                }
            }
        }
        //减去优惠总价
        BigDecimal subtract = amount.subtract(getReduce());
        return subtract;
    }

    public BigDecimal getReduce() {
        return reduce;
    }

    public void setReduce(BigDecimal reduce) {
        this.reduce = reduce;
    }
}
